<template>
  <el-dialog
    v-model="dialogVisible"
    title="确认同步"
    width="400px"
    @close="closeDialog"
  >
    <div>是否确认同步"{{ invclInfo.name }}"下的ERP存货档案？</div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { localStorageConfig } from '@/config/localstorage.config'
  import { syncInventoryApi } from '@/api/base/inventory/index'
  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const dialogVisible = ref(false)

  const invclInfo = ref()

  const openDialog = (value) => {
    invclInfo.value = value
    dialogVisible.value = true
  }

  const confirm = async () => {
    const load = $baseLoading()
    try {
      let params = {
        invclCode: invclInfo.value.invclCode,
        pkCorp: pkCorp,
      }
      console.log(params)
      const { code, msg } = await syncInventoryApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
    dialogVisible.value = false
    _emit('success-callback')
  }

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
